package com.genius.unite_platform.storedevice.service.impl;


import java.sql.SQLException;
import java.util.HashMap;

import javax.servlet.http.HttpSession;

import org.apache.poi.util.SystemOutLogger;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.menu.entity.MenuJson;
import com.genius.adminmanager.menu.service.MenuUtils;
import com.genius.adminmanager.other.entity.FillAddJson;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.other.entity.SessionJson;
import com.genius.dingding.util.GeniusConfig;
import com.genius.workflow.page.PageResult;
import com.genius.workflow.page.SearchResult;
import com.genius.workflow.page.SqlLang;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools;
import com.genius.workflow.util.DBTools2;
import com.genius.unite_platform.storedevice.service.StoreDeviceServiceAdapter;


@Service("StoreDeviceSelectService")
	public class StoreDeviceSelectServiceImpl extends StoreDeviceServiceAdapter{
	String CAIJIDATABASE = GeniusConfig.CAIJIDATABASE;  //jhcvs
	@Override
	public JSONObject getSelectHead(JSONObject json, HttpSession session,JSONObject response) {
		String menuId = json.getString(MenuJson.menuId);

		String roleId = (String)session.getAttribute(SessionJson.roleId);

		response = MenuUtils.getPowerFileByMenu(roleId,menuId, response);

		JSONObject result =  response.getJSONObject(ResponseJson.data);
		if(result == null){
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_No_Porwer_Query_Field);
			return response;
		}
		return response;
	}

	@Override
	public JSONObject getSelectBody(JSONObject json, JSONObject response) {
		int limit = json.getIntValue(PageResult.limit);
        int page = json.getIntValue(PageResult.page);
		SqlLang sql = null;
		System.out.println(json);
		String storeno= "";
		String storename="";
		String devicename= "";
		String modelname= "";	
			
		if (json.containsKey("search"))
	    {	
	       storeno=(json.getJSONObject("search").getString("storeno")==null?"":json.getJSONObject("search").getString("storeno"));
	       storename=(json.getJSONObject("search").getString("storename")==null?"":json.getJSONObject("search").getString("storename"));
	       devicename=(json.getJSONObject("search").getString("devicename")==null?"":json.getJSONObject("search").getString("devicename"));
	       modelname=(json.getJSONObject("search").getString("modelname")==null?"":json.getJSONObject("search").getString("modelname"));
	    }			
	
        String selectSB = " select a.storeno, d.deptname storename,a.deviceno,b.devicename,a.modelId,c.modelname,c.modeltype,a.devicenum , c.imgs,a.memo, a.lrr , a.lrsj  "
        		        + " from  storedevice a,device  b,devicemodel c,(select deptcode,deptname from "+CAIJIDATABASE+".dict_department where type=1)  d"
        		        + " where  a.deviceno=b.deviceno "
        		        + "	and    a.modelId=c.modelId "
        		        + "	and    b.deviceno=c.deviceno  "
        		        + " and    a.storeno=d.deptcode"
        		        + "	and    a.storeno like '%"+storeno+"%'  "
        		        + " and    d.deptname like '%"+storename+"%' "
        		        + " and    b.devicename like '%"+devicename+"%'"
        		        + " and    c.modelname like '"+modelname+"%'";
        		  //      + " order by a.storeno,a.deviceno,a.modelId";
        //System.out.println(selectSB);   
        
        String headField = json.getString(PageResult.headField);
	
		JSONObject parmeterJson = DBTools2.getWhereFieldByParameter(json);
		
		SearchResult  searchResult = DBTools2.getWhereFieldBySearch(json);
		
		JSONObject mergeJson = DBTools2.mergeAll(parmeterJson, searchResult);
		if(json.containsKey(PageResult.asc)){
			String ascField = json.getString(PageResult.ascField);
			String asc = json.getString(PageResult.asc);
			sql = DBTools2.getSelectPageSql(headField,selectSB,page,limit,ascField,asc,searchResult.getSearchWhere());
		}else{
			String asc = "asc";
			String ascField = "storeno";
			sql = DBTools2.getSelectPageSql(headField,selectSB,page,limit,ascField,asc,searchResult.getSearchWhere());
		}
        try {
			String parameterArr [] = DBTools2.parsePermeterBySql(sql.getSourceSql(), mergeJson);
			PageResult result = DBTools2.selectByCount(sql,parameterArr);
			JSONArray data = (JSONArray)result.getData();
			response.put(ResponseJson.count, result.getCount());
			response.put(ResponseJson.data, data);
			response.put(ResponseJson.status, ResponseJson.status_Success);
        } catch (ConnectionException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
        } catch (DBClosedException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
        } catch (SQLException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Query_Data_Error);
        }
		return response;
	}	
}



	